package leetcode.view0108;

public class Solution {
	public void setZeroes(int[][] matrix) {
		int m = matrix.length, n = matrix[0].length;
		boolean row = false, col = false;
		int p = 0;
		while (!row && p < m) {
			if (matrix[p++][0] == 0) {
				row = true;
			}
		}
		p = 0;
		while (!col && p < n) {
			if (matrix[0][p++] == 0) {
				col = true;
			}
		}
		for (int i = 1; i < m; i++) {
			for (int j = 1; j < n; j++) {
				if (matrix[i][j] == 0) {
					matrix[i][0] = 0;
					matrix[0][j] = 0;
				}
			}
		}
		matrix[0][0] = row || col ? 0 : matrix[0][0];
		p = 1;
		while (p < m) {
			if (matrix[p][0] == 0) {
				for (int i = 1; i < n; i++) {
					matrix[p][i] = 0;
				}
			}
			p++;
		}
		p = 1;
		while (p < n) {
			if (matrix[0][p] == 0) {
				for (int i = 1; i < m; i++) {
					matrix[i][p] = 0;
				}
			}
			p++;
		}
		p = 0;
		while (row && p < m) {
			matrix[p++][0] = 0;
		}
		p = 0;
		while (col && p < n) {
			matrix[0][p++] = 0;
		}
	}
}
